package com.pb.camera.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.reflect.TypeToken;
import com.pb.camera.bean.Equipment;
import com.pb.camera.helper.JsonHelper;

import android.app.Activity;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.Toast;

public class JsonAnalyle {

	public static String[] keys = new String[] { "-1", "0", "50001", "50002",
			"50003", "50004", "50005", "50006", "50007", "50008", "50009",
			"50010", "50011", "50012", "50013", "50014", "50015", "50016",
			"50017", "50018", "50019", "50020", "50021", "50022", "50023",
			"50024", "50025" };
	public static String[] notice = new String[] { "系统繁忙", "请求成功",
			"该手机号已注册，请直接登录！", "手机号不存在", "用户账号已注册，请尝试其他用户名！", "验证码不匹配",
			"验证码已失效", "未绑定系统用户！", "账号或密码不正确，登录失败", "设备已添加到其他分组",
			"分组添加网关时，分组中已有网关", "分组添加终端时，分组中无网关", "用户不存在", "用户已在其他移动设备登录APP",
			"用户已被邀请", "不能邀请自己", "此账户已绑定其他授权微信帐号", "此授权微信账号已绑定其他账户", "网络异常",
			"同组无红外转发器", "添加失败", "删除失败", "发送控制失败", "发送红外码失败",
			"不是智能插座或者该智能插座不在该组中", "入网失败！", "开始学习"
	};

	public static boolean jsonAnalyse(String data, final Context context,
			String key, Handler handle) {
		boolean flag = false;
		Map map = JsonHelper.jsonStringToMap(data, new String[] { "errcode" },
				key);
		String tem = (String) map.get("errcode");
		for (int i = 0; i < keys.length; i++) {
//			if (i == 0) {
//				tem = (String) map.get("errcode");
//			}
			if (tem.equals(keys[i])) {
				if (i == 6) {
					Map map_mes = JsonHelper.jsonStringToMap(data,
							new String[] { "errmsg" }, key);
					String mess = (String) map_mes.get("errmsg");
					if (mess.contains("mobile")) {
						if (handle != null && i != 1) {
							Message mes = Message.obtain();
							mes.arg2 = 6;
							mes.obj = "手机号不存在";
							handle.sendMessage(mes);
						}
						return false;
					}
				}
				if (handle != null && i != 1) {
					Message mes = Message.obtain();
					mes.arg2 = 6;
					mes.obj = notice[i];
					handle.sendMessage(mes);
				}
				if (handle == null && context != null && i != 1) {
					if (context instanceof Activity) {
						final int noticePos = i;
						Activity ac = (Activity) context;
						ac.runOnUiThread(new Runnable() {
							@Override
							public void run() {
								// TODO Auto-generated method stub
								Toast.makeText(context, notice[noticePos],
										Toast.LENGTH_SHORT).show();
							}
						});
					}
				}
				if (keys[i].equals("0")) {
					flag = true;
					return true;
				}
			}
		}
		return false;
	}

	public static String getErrcode(String data, String key) {

		Map map = JsonHelper.jsonStringToMap(data, new String[] { "errcode" },
				key);

		return (String) map.get("errcode");

	}

	public static List<Map<String, Object>> getIfLinkaged(String data,
			String key) {
		String[] keyNames = { "devid", "linkage_way", "linkage_flag",
				"last_status", "param", "switch" };
		List<Map<String, Object>> list = JsonHelper.jsonStringToList(data,
				keyNames, "data");
		// list 可能返回的内容是0个
		return list;
	}

	public static List<Map<String, Object>> getIfAirconditionLinkaged(
			String data, String key) {
		String[] keyNames = { "devid", "linkage_way", "linkage_flag",
				"last_status", "param" };
		List<Map<String, Object>> list = JsonHelper.jsonStringToList(data,
				keyNames, "data");
		// list 可能返回的内容是0个
		return list;
	}

	public static Map registerSuccess(String data) {
		String[] keys = { "userid", "hsacco", "openid" };
		Map map = JsonHelper.jsonStringToMap(data, keys, null);
		return map;
	}

	public static Map logSuccess(String data) {
		String[] keys = { "userid", "hsacco", "openid", "gesture", "mobile",
				"email", "account" };
		Map map = JsonHelper.jsonStringToMap(data, keys, null);
		return map;
	}

	public static Map ifWeiXinbinded(String data) {
		String[] keys = { "userid", "hsacco", "openid", "gesture", "mobile",
				"email", "p", "acco" };
		Map map = JsonHelper.jsonStringToMap(data, keys, null);
		return map;
	}

	public static Map weiXinLogSuccess(String data) {
		String[] keys = { "userid", "hsacco", "p" };
		Map map = JsonHelper.jsonStringToMap(data, keys, null);
		return map;
	}

	public static Map gateWayInfo(String data) {
		String[] keys = { "voltage", "mtime", "devType" };
		Map map = JsonHelper.jsonStringToMap(data, keys, null);
		return map;
	}

	/**
	 * @param data
	 * @return 获取的是设备的当前信息 ,后台会跟硬件交互,把硬件的实时信息返回过来
	 */
	public static Map environmentDetectorInfo(String data) {
		String[] keys = { "val_pm", "val_tmp", "val_hum", "val_voc", "val_lit",
				"val_vol", "mtime", "devType" };
		Map map = JsonHelper.jsonStringToMap(data, keys, null);
		return map;
	}

	public static Map socketInfo(String data) {

		String[] keys = { "switch", "mtime", "devType" };
		Map map = JsonHelper.jsonStringToMap(data, keys, null);

		return map;
	}


	public static List<Map<String, Object>> getUnbinedSocketFromGroup(
			String data) {
		String[] keyNames = { "devid", "name" };
		// JsonHelper.jsonStringToMap(data, new String[], key)
		List<Map<String, Object>> list = JsonHelper.jsonStringToList(data,
				keyNames, "data");

		return list;
	}

	public static List<Map<String, Object>> getAllAirConditionName(String data) {
		String[] keyNames = { "devbrand", "number" };
		// JsonHelper.jsonStringToMap(data, new String[], key)
		List<Map<String, Object>> list = JsonHelper.jsonStringToList(data,
				keyNames, "data");
		Log.i("123", "空调的品牌的数据" + list.size());
		return list;
	}

	public static Map<String, Object> getRemoteControlLinkAgeOutcome(String data) {
		String[] keys = { "rc", "ls" };
		return JsonHelper.jsonStringToMap(data, keys, null);

	}

	public static List<Map<String, Object>> getTimerTask(String data) {
		String[] keys = { "on_time", "off_time", "is_task_on", "weekday", "no" };
		// JSONObject jsonOb=null;
		// JSONArray jsonArr=null;
		// try {
		// jsonOb = new JSONObject(data);
		// jsonArr=jsonOb.getJSONArray("data");
		// Log.i("===","length"+ jsonArr.length());
		// } catch (JSONException e) {
		// // TODO Auto-generated catch block
		// e.printStackTrace();
		// }

		return JsonHelper.jsonStringToList(data, keys, "data");
	}



	public static Map<String, Object> getCurrentCityDate(String data) {
		// {"errcode":0,"errmsg":"success"data:{"update":"xxx","hum":"xxx","tmp":"xxx","pm25":"xxx"}}

		String[] keys = { "update", "hum", "tmp", "pm25" };
		Map<String, Object> dataMap = JsonHelper.jsonStringToMap(data, keys,
				"data");

		return dataMap;
	}



	public static Map<String, Object> getSelectedEnvironmentModeData(String data) {

		String[] keys = { "selected" };
		Map<String, Object> dataMap = JsonHelper.jsonStringToMap(data, keys,
				null);

		return dataMap;
	}
	
	public   static   Map<String ,Object>  addRoom(String json)
	{
		  String [] keys={"gid","gname"};
	    return	JsonHelper.jsonStringToMap(json, keys, null);
	}
	public   static   List<Equipment> getEquipment(String json)
	{
		JSONObject  jo;
		String  ja;
		try {
		   jo=new JSONObject(json);
		   ja=jo.getJSONArray("data").toString();
		   Gson gson = new Gson();
			List<Equipment> dataList = (List<Equipment>) gson.fromJson(ja,
					new TypeToken<List<Equipment>>() {
					}.getType());
			
			return   dataList;
	 	} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
		return  new ArrayList<Equipment>();
	}
}
